package xiangya.bysj.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.taobao.api.domain.Userinfos;

import xiangya.bysj.entity.Doctor;
import xiangya.bysj.entity.RankDoctor;
import xiangya.bysj.mapper.interf.CentMapper;
import xiangya.bysj.mapper.interf.DoctorMapper;
import xiangya.bysj.service.interf.DoctorServiceInterface;

/**
 * doctor业务类
 * 
 * @author Mr.zhang
 *
 */
@Service("doctorService") // 注册业务类
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class) // 注明需要回滚
public class DoctorServiceImpl implements DoctorServiceInterface {
	@Resource
	//积分mapper 
	private CentMapper centMapper;
	
	// 加上自动注入的注解或者用@Resource(name="doctorMapper")Name的值必须与映射文件的名字对应
	@Autowired
	private DoctorMapper doctorMapper;
	

	
	@Resource(name = "IMService")
	private IMServiceImpl IMService;
	
	/**
	 * 保存对象
	 */
	@Override
	public void save(Doctor obj) throws Exception {
		if (obj != null) {
			doctorMapper.save(obj);
			// 建立百川上面的用户类
			Userinfos userinfos = new Userinfos();
			userinfos.setUserid(obj.getDoctorCode());
			userinfos.setPassword(obj.getDoctorPass());
			userinfos.setNick(obj.getDoctorName());
			IMService.save(userinfos);
			//建立积分账号 
			centMapper.registerCent(obj.getDoctorCode());
		}else{
			throw new RuntimeException();
		}
	}

	/**
	 * 登陆
	 */
	@Override
	public Doctor login(String doctorCode, String doctorPass) throws Exception {
		Doctor role = doctorMapper.selectByCode(doctorCode);
		if (role != null && (!role.getDoctorPass().equals(doctorPass) || role.getDoctorStatus() != 1)) {
			role = null;
		}
		return role;
	}

	/**
	 * 更新
	 */
	@Override
	public void update(Doctor obj) throws Exception {
		doctorMapper.update(obj);
		// 在百川数据库中更新(只能修改密码)
		if (obj.getDoctorPass() != null && !obj.getDoctorPass().equals("")) {
			Userinfos userinfos = new Userinfos();
			userinfos.setUserid(obj.getDoctorCode());
			userinfos.setPassword(obj.getDoctorPass());
			IMService.update(userinfos);
		}
	}

	/**
	 * 删除
	 */
	@Override
	public void delete(int objId) throws Exception {
		// doctorMapper.delete(objId);
	}

	/**
	 * 根据名字模糊查询
	 */
	@Override
	public List<Doctor> getSome(String doctorName, int doctorStatus) throws Exception {
		List<Doctor> doctors = doctorMapper.selectByPartName(doctorName, doctorStatus);
		return doctors;
	}

	/**
	 * 根据登陆账号删除
	 */
	@Override
	public void deleteByCode(String doctorCode) throws Exception {
		doctorMapper.deleteByCode(doctorCode);
		// 删除百川那边的账号
		IMService.deleteByCode(doctorCode);
	}

	public Doctor getById(int doctorid) throws Exception {

		return doctorMapper.selectById(doctorid);
		
		
	}
	
	public RankDoctor getRankById(int id) throws Exception {
		
		return centMapper.selectRankByDoctorId(id);
		
		
	}
	
	public List<RankDoctor> get10Top() throws Exception {
		
		return centMapper.selectRank10Top();
		
		
	}
	
	
	public void updateDoctorStatus(int doctorId,int doctorStatus) throws Exception {
		doctorMapper.updateDoctorStatus(doctorId, doctorStatus);
	}
	
	
	public void updateDoctorDiploma(int doctorId,String doctorDiploma) throws Exception {
		doctorMapper.updateDoctorDiploma(doctorId, doctorDiploma);
	}
	
	
	public List<Doctor> selectUncheckedDoctor(int offset) throws Exception {
		return doctorMapper.selectUncheckedDoctor(offset);
		
	}
	public int selectUncheckedDoctorCount() throws Exception {
		return doctorMapper.selectUncheckedDoctorCount();
		
	}

	public void updateCent(String doctorCode,double cent) {
		centMapper.updateCent("forAnswer", doctorCode, cent);
	}
	
}
